﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class UI_Item : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties

	public bool show_Name;
	public bool show_Value;
	public bool show_Quantity;
	public bool show_Weight;
	public bool isPlayerInventory;
	
	//---------------------------------------------------------------------------------------------
	// Members
	
	public Item m_item;
	public Text m_itemName;
	public Text m_itemCost;
	public Text m_itemQuantity;
	public Text m_itemWeight;
	public Button m_consumeButton;
	public Button m_buyButton;
	public Button m_sellButton;
	public Character m_playerMain;
	public Inventory m_inventoryMain;
	
	
	
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	
	void OnEnable()
	{
		m_inventoryMain = GameObject.Find ("PlayerCharacter").GetComponent<Inventory>();
		
		// show properties of the field
		if (show_Name != false)
		{
			if (transform.FindChild("Text - ItemName").gameObject.GetComponent<Text>() != null)
			{
				m_itemName = transform.FindChild("Text - ItemName").gameObject.GetComponent<Text>();
			}
		}
		
		if (show_Quantity != false)
		{
			if (transform.FindChild("Text - ItemQuantity").gameObject.GetComponent<Text>() != null)
			{
				m_itemQuantity = transform.FindChild("Text - ItemQuantity").gameObject.GetComponent<Text>();
			}
		}
		
		if (show_Weight != false)
		{
			if (transform.FindChild("Text - ItemWeight").gameObject.GetComponent<Text>() != null)
			{
				m_itemWeight = transform.FindChild("Text - ItemWeight").gameObject.GetComponent<Text>();
			}
		}
		
		if (show_Value != false)
		{
			if (transform.FindChild("Text - ItemValue").gameObject.GetComponent<Text>() != null)
			{
				m_itemCost = transform.FindChild("Text - ItemValue").gameObject.GetComponent<Text>();
			}
		}
		
		
		if (isPlayerInventory == true)
		{
			// is the button active?
			if (m_item.isConsumable != false)
			{
				m_consumeButton = transform.FindChild("Button - Consume").gameObject.GetComponent<Button>();
				m_consumeButton.gameObject.SetActive(true);
			}
			else 
			{
				m_consumeButton = transform.FindChild("Button - Consume").gameObject.GetComponent<Button>();
				m_consumeButton.gameObject.SetActive(false);
			}		
			
			// is the item consumable? 
			if (m_item.isWater)
			{
				
				m_consumeButton.onClick.AddListener(() => { DrinkWater(); m_inventoryMain.removeFromInventory(m_item); Debug.Log ("HOShit"); });
				
			}
			
			if (m_item.isFood)
			{
				m_consumeButton.onClick.AddListener(() => { EatFood(); m_inventoryMain.removeFromInventory(m_item); Debug.Log ("HOShit"); });
				
			}
		}
		
		if (isPlayerInventory == false)
		{
			m_buyButton = transform.FindChild("Button - Buy").gameObject.GetComponent<Button>();
			m_buyButton.gameObject.SetActive(true);
			
			m_sellButton = transform.FindChild("Button - Sell").gameObject.GetComponent<Button>();
			m_sellButton.gameObject.SetActive(true);
			
			m_playerMain = GameObject.Find ("PlayerCharacter").GetComponent<Character>();
			m_inventoryMain = m_playerMain.m_currentTerrain.GetComponent<Inventory>();
			
			m_buyButton.onClick.AddListener(() => {m_inventoryMain.BuyItem(m_item);Debug.Log ("HOShit");});
			m_sellButton.onClick.AddListener(() => {m_inventoryMain.SellItem(m_item);Debug.Log ("HOShit");});
			
			
			
			
		
		}
		
		
		// subscibe buttons to functions
	
		
		
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions

	void Update () 
	{
		if (m_itemCost != null)
		{
			m_itemCost.text = m_item.Value.ToString();
		}
		if (m_itemName != null)
		{
			m_itemName.text = m_item.Name;
		}
		if (m_itemQuantity != null)
		{
			m_itemQuantity.text = m_item.Quantity.ToString();
		}
		if (m_itemWeight != null)
		{
			m_itemWeight.text = m_item.Weight.ToString();
		}
	
		
	}
	
	//---------------------------------------------------------------------------------------------
	public void EatFood()
	{
		// these functions need checks to stop the player from consuming the item when they've reached max food drink or health.
		m_item.eatFood();
	
	}
	
	//---------------------------------------------------------------------------------------------
	public void DrinkWater()
	{
		m_item.drinkWater();
	}
	
	//---------------------------------------------------------------------------------------------

}
